package com.xuchaotong.zk.demo;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.soap.Node;
import java.io.IOException;

/**
 * @author 311633
 * @version 1.0
 * @ClassName:
 * @Description:
 * @date 2017/11/20 10:52
 */
public class NodeWatcher implements Watcher {
    private static final Logger LOGGER = LoggerFactory.getLogger(NodeWatcher.class);
    private String monitorPath = "/nodeRoot";

    public NodeWatcher() {

    }

    public void monitorMaster() {
        Stat stat = new Stat();
        try {


            ZookeeperMgr.getInstance().read(monitorPath, this, stat);

        } catch (InterruptedException e) {

            LOGGER.info(e.toString());

        } catch (KeeperException e) {
            LOGGER.error("cannot monitor " + monitorPath, e);
        }
    }

    public void process(WatchedEvent event) {
        if (event.getType() == Event.EventType.NodeDataChanged) {
            try {
                LOGGER.info("============GOT UPDATE EVENT " + event.toString() + ": (" + monitorPath + ",)======================");
                Stat stat = new Stat();
                ZookeeperMgr.getInstance().read(monitorPath, this, stat);
            } catch (Exception e) {

                LOGGER.error("monitor node exception. " + monitorPath, e);
            }
        }

    }
}
